<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="eidt-el-form">
            <el-form-item label="名称" >
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="码值" prop="code">
              <el-input v-model="ruleForm.code"></el-input>
            </el-form-item>
            <el-form-item label="编号" >
              <el-input v-model="ruleForm.idNumber"></el-input>
            </el-form-item>
            <el-form-item label="证件类型" prop="documentTypeId">
              <el-select v-model="ruleForm.documentTypeId" placeholder="选择证件类型">
                <el-option
                  v-for="(type,index) in DocumentType"
                  :label="type.name"
                  :key="index"
                  :value="type.documentTypeId">
                  <div @click="getChangeType(type)">{{type.name}}</div>
                </el-option>
              </el-select>
            </el-form-item>
            <UploadImg @uploadImg="getUploadImg" :editImg="editImg" :imgTitle="imgTitle"></UploadImg>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="ruleForm.remark" ></el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>
import UploadImg from '../../public/UploadImg'

export default {
  name: "editprojectmanage",
  data() {
    return {
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        code: [{ required: true, message: "码值不能为空", trigger: "blur" }],
        idNumber: [{ required: true, message: "编号不能为空", trigger: "blur" }],
        documentTypeId:[{ required: true, message: "请选择证件类型", trigger: "blur" }]
      },
      ruleForm: {
        name: "",
        documentTypeId:"",
        code: "",
        idNumber:"",
        typeName:"",
        avatar:"",
        projectId:''
      },
      editImg:"",
      document_Id: "",
      dataList:[],
      ticketTypeId:'',
      DocumentType:[],
      imgTitle:'上传头像'
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object
    }
  },
  created(){
    this.projectId = localStorage.getItem('project_Id')
    this.getTicketType();
  },
  watch: {
    editData: function() {
      const item = this.editData;
      this.ruleForm = {
        name:item.name,
        remark: item.remark,
        code: item.code,
        idNumber: item.idNumber,
        documentTypeId: item.documentTypeId,
        remark: item.remark,
        typeName: item.typeName,
      };
      this.document_Id = item.documentId 
      this.editImg = item.avatar;
    }
  },
  methods: {
    saveClick(ruleForm) {
      this.ruleForm = {
        name: this.ruleForm.name,
        code: this.ruleForm.code,
        remark: this.ruleForm.remark,
        idNumber: this.ruleForm.idNumber,
        projectId:this.projectId,
        typeName: this.ruleForm.typeName,
        avatar: this.ruleForm.avatar,
        documentTypeId: this.ruleForm.documentTypeId
      };
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (this.document_Id == '' || this.document_Id == undefined) {
            this.$axios.post("/Document/", this.ruleForm).then(res => {
              if (res.data.hasError == false) {
                this.$message.success("保存成功！");
                this.goBack();
                this.$refs[ruleForm].resetFields();
              } else {
                this.$message.error("保存失败！");
              }
            });
          } else {
            this.$axios.put("/Document/" + this.document_Id + "", this.ruleForm)
              .then(res => {
                if (res.data.hasError == false) {
                  this.$message.success("编辑成功！");
                  this.goBack();
                  this.$refs[ruleForm].resetFields();
                } else {
                  this.$message.error("编辑失败！");
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$emit("backFunStu");
      this.ruleForm.avatar = '';
      this.ruleForm.documentTypeId="";
      this.document_Id = '';
    },
    getUploadImg(imgSrc){
      this.ruleForm.avatar = imgSrc;
    },
    getTicketType(){
      this.$axios.get('/DocumentType/Project/'+this.projectId).then(res =>{
        if (res.data.hasError == false) {
          this.DocumentType = [...res.data.data]
        }
      })
    },
    getChangeType(type){
      this.ruleForm.documentTypeId = type.documentTypeId;
      this.ruleForm.typeName = type.name;
    }
  },
  components: {
    UploadImg
  }
};
</script>